/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.provider;


import admin.result.IResult;
import admin.result.ResultFail;
import admin.result.ResultSuccess;
import ejb.attribute.ParSearchDoubleRange;
import ejb.attribute.ParSearchEqual;
import ejb.attribute.ParSearchString;
import ejb.attribute.ResultRange;
import ejb.bean.AbstractFacade;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Nothing4ever
 */
public abstract class WrapperProvider<T> {

    private Class<T> entityClass;
    private final String ACTION_ADD = "Thêm";
    private final String ACTION_UPDATE = "Cập nhật";
    private final String ACTION_REMOVE = "Xóa";

    public WrapperProvider(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract AbstractFacade<T> getAbstractFacade();

    public T findById(Object id) {
        return getAbstractFacade().find(id);
    }

    public List<T> findAll() {
        return getAbstractFacade().findAll();
    }

    public List<T> findRange(ResultRange resultRange) {
        return getAbstractFacade().findRange(resultRange);
    }

    public List<T> searchBasic(T t, ResultRange resultRange) {
        return getAbstractFacade().searchBasic(t, resultRange);
    }

    public List<T> searchAdvance(ParSearchString parSearchString, ParSearchDoubleRange parSearchDoubleRange,
            ParSearchEqual parSearchEqual, ResultRange resultRange) {
        List<ParSearchString> listParSearchString = new ArrayList<ParSearchString>();
        if (parSearchString != null) {
            listParSearchString.add(parSearchString);
        }
        List<ParSearchDoubleRange> listParSearchDoubleRange = new ArrayList<ParSearchDoubleRange>();
        if (parSearchDoubleRange != null) {
            listParSearchDoubleRange.add(parSearchDoubleRange);
        }
        List<ParSearchEqual> listParSearchEqual = new ArrayList<ParSearchEqual>();
        if (parSearchEqual != null) {
            listParSearchEqual.add(parSearchEqual);
        }
        return getAbstractFacade().searchAdvance(listParSearchString, listParSearchDoubleRange, listParSearchEqual, resultRange);
    }

    public List<T> searchAdvance(List<ParSearchString> listParSearchString,
            List<ParSearchDoubleRange> listParSearchDoubleRange,
            List<ParSearchEqual> listParSearchEquals,
            ResultRange resultRange) {
        return getAbstractFacade().searchAdvance(listParSearchString, listParSearchDoubleRange, listParSearchEquals, resultRange);
    }

    public int count() {
        return getAbstractFacade().count();
    }

    public IResult add(T t) {
        try {
            getAbstractFacade().create(t);
            return new ResultSuccess(ACTION_ADD, getObjectName());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultFail(ACTION_ADD, getObjectName());
        }

    }

    public IResult update(T t) {
        try {
            getAbstractFacade().edit(t);
            return new ResultSuccess(ACTION_UPDATE, getObjectName());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultFail(ACTION_UPDATE, getObjectName());
        }
    }

    public IResult remove(T t) {
        try {
            getAbstractFacade().remove(t);
            return new ResultSuccess(ACTION_REMOVE, getObjectName());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultFail(ACTION_REMOVE, getObjectName());
        }
    }

    protected String getObjectName() {
        return entityClass.getSimpleName();
    }
}
